package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.GoodsDao;
import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartDao dao;

    @Autowired
    GoodsDao goodsDao;
    @Override
    public Cart findCartByUIdAndGoodsId(int id, int goodsId) {
        return dao.findCartByUIdAndGoodsId(id, goodsId);
    }

    @Override
    public void insert(Cart cart) {
        dao.insert(cart);
    }

    @Override
    public void update(Cart cart) {
        dao.update(cart);
    }

    @Override
    public List<Cart> getCarts(int id) {
        //获取购物车
        List<Cart> carts = dao.getCarts(id);

        if (carts.size() > 0) {
            //遍历购物车
            for (Cart cart : carts) {

                int getpId = cart.getpId();
                Goods goods = goodsDao.findById(getpId);
                cart.setGoods(goods);
            }
        }
        return carts;
    }

    @Override
    public void deleteCart(int id,int goodsId) {
        dao.deleteCart(id,goodsId);
    }

    @Override
    public void clearCart(int id) {
        dao.clearCart(id);
    }
}
